<template>
	<view class="user" :style="[background]">
		<!-- #ifndef  H5 -->
		<u-sticky offset-top="0" h5-nav-height="0" bg-color="transparent">
			<u-navbar :is-back="false" title="个人中心" :title-bold="true" :is-fixed="false" :border-bottom="false"
				:background="{ background: 'rgba(256,256, 256,' + navBg + ')' }"
				:title-color="navBg > 0.5 ? '#000' : '#fff'"></u-navbar>
		</u-sticky>
		<!-- #endif -->
		<view class="header">
			<view class="hd-wrap bg-white">
				<view class="user-info flex row-between">
					<router-link to="/bundle/pages/user_profile/user_profile">
						<view class="info flex">
							<image class="avatar m-r-20 flex-none"
								:src="isLogin ? userInfo.avatar : '/static/images/portrait_empty.png'" />
							<view v-if="isLogin">
								<view class="name">
									<view class="xxl bold line-2">{{userInfo.nickname}}</view>
									<view class="level" @tap.stop="openLevel">
										<image v-if="userInfo.is_shop === 1" style="width: 50rpx;" :src="vipImgUrl[6]" mode="widthFix"></image>
										<image v-else style="width: 50rpx;" :src="vipImgUrl[userInfo.level]" mode="widthFix"></image>
									</view>
								</view>
								<view class="flex m-t-10" v-if="userInfo.sn">
									<view class="user-id xs m-r-20">会员ID: {{userInfo.sn}}</view>
									<view class="xs primary row-center m-l-5" @tap.stop="onCopy">复制</view>
								</view>
							</view>
							<view v-else>
								<view style="font-size: 42rpx">点击登录</view>
								<view class="sm m-t-10 lighter">登录体验更多功能</view>
							</view>
						</view>
					</router-link>
					<view class="flex m-l-20">
						<router-link to="/bundle/pages/user_profile/user_profile">
							<view class="user-opt">
								<image style="width:58rpx;height: 58rpx;" src="/static/images/icon_my_setting.png">
								</image>
							</view>
						</router-link>
					</view>
				</view>
				<view class="user-assets flex m-t-20 m-b-20">
					<router-link class="user-assests-item" to="/bundle/pages/user_wallet/user_wallet">
						<view class="flex-col col-center">
							<view class="xl primary">
								{{userInfo.user_money || 0}}
							</view>
							<view class="sm m-t-10">
								余额
							</view>
						</view>
					</router-link>
					<router-link class="user-assests-item" to="/bundle/pages/integral_details/integral_details">
						<view class="flex-col col-center">
							<view class="xl primary">
								{{userInfo.user_integral || 0}}
							</view>
							<view class="sm m-t-10">
								积分
							</view>
						</view>
					</router-link>
					<view class="user-assests-item">
						<view class="flex-col col-center">
							<view class="xl primary">
								{{userInfo.backfall_mun || 0}}
							</view>
							<view class="sm m-t-10">
								销售累积
							</view>
						</view>
					</view>
					<!-- <router-link class="user-assests-item" to="/bundle/pages/user_coupon/user_coupon">
						<view class="flex-col col-center">
							<view class="xl primary">
								{{userInfo.coupon || 0}}
							</view>
							<view class="sm m-t-10">
								优惠券
							</view>
						</view>
					</router-link> -->
				</view>
			</view>
		</view>

		<view class="order-nav bg-white">
			<router-link to="/bundle/pages/user_order/user_order">
				<view class="title flex row-between">
					<view class="lg">我的订单</view>
					<view class="muted sm row">
						全部订单
						<u-icon name="arrow-right" size="28rpx" />
					</view>
				</view>
			</router-link>
			<view class="nav flex">
				<router-link class="item" :to="{path: '/bundle/pages/user_order/user_order',query:{type: 'pay'}}">
					<view class="flex-col col-center m-b-20">
						<view class="icon-contain">
							<view v-if="userInfo.wait_pay" class="badge xs flex row-center bg-white">
								{{userInfo.wait_pay}}
							</view>
							<image class="nav-icon" src="/static/images/icon_my_payment.png"></image>
						</view>
						<view class="sm m-t-10">待付款</view>
					</view>
				</router-link>
				<router-link class="item" to="/bundle/pages/user_order/user_order?type=delivery">
					<view class="flex-col col-center m-b-20">
						<view class="icon-contain">
							<view v-if="userInfo.wait_delivery" class="badge xs flex row-center bg-white">
								{{userInfo.wait_delivery}}
							</view>
							<image class="nav-icon m-b-10" src="/static/images/icon_my_fahuo.png"></image>
						</view>
						<view class="sm">待发货</view>
					</view>
				</router-link>
				<router-link class="item" to="/bundle/pages/user_order/user_order?type=delivery">
					<view class="flex-col col-center m-b-20">
						<view class="icon-contain">
							<view v-if="userInfo.wait_take" class="badge xs flex row-center bg-white">
								{{userInfo.wait_take}}
							</view>
							<image class="nav-icon" src="/static/images/icon_my_shouhuo.png"></image>
						</view>
						<view class="sm m-t-10">待收货</view>
					</view>
				</router-link>
				<router-link class="item" to="/bundle/pages/user_comment/user_comment">
					<view class="flex-col col-center m-b-20">
						<view class="icon-contain">
							<view v-if="userInfo.wait_comment" class="badge xs flex row-center bg-white">
								{{userInfo.wait_comment}}
							</view>
							<image class="nav-icon" src="/static/images/icon_my_pingjia.png"></image>
						</view>
						<view class="sm m-t-10">商品评价</view>
					</view>
				</router-link>
				<router-link class="item" to="/bundle/pages/after_sales/after_sales">
					<view class="flex-col col-center m-b-20">
						<view class="icon-contain">
							<view v-if="userInfo.after_sale" class="badge xs flex row-center bg-white">
								{{userInfo.after_sale}}
							</view>
							<image class="nav-icon" src="/static/images/icon_my_shouhou.png"></image>
						</view>
						<view class="sm m-t-10">退款/售后</view>
					</view>
				</router-link>
			</view>
		</view>
		<view class="server-nav bg-white" v-if="menuList && menuList.length > 0">
			<view>
				<view class="title flex row-between">
					<view class="lg">我的功能</view>
				</view>
			</view>
			<view class="nav flex flex-wrap">
				<view v-for="(item, index) in menuList" :key="index" class="item flex-col col-center m-b-20"
					style="width: 25%;" @tap="menuJump(item)">
					<image class="nav-icon" :src="item.image"></image>
					<view class="sm m-t-10">{{item.name}}</view>
				</view>
			</view>
		</view>
		<view v-if="userInfo.is_new !== 1 || userInfo.is_new2 !== 1" class="free-box" @click="openFree">
			<image style="width: 120rpx;" src="@/static/images/free-img.png" mode="widthFix"></image>
		</view>
		<goods-column ref="mescrollItem"></goods-column>
		<FreeBlindBox :showPop="isShowFreeBlindBox" :userInfo="userInfo" :freeGoodsList="freeGoodsList" :closeFreeBlindBox="closeFreeBlindBox" />
		<u-popup v-model="specificationPopup" mode="center">
			<view class="rule-box">
				<view class="rule-title">
					等级说明
				</view>
				<view class="rule-table">
					<table cellpadding='5'>
						<tr>
							<th>等级</th>
							<th>升级条件（收入）</th>
							<th>奖励（每单）</th>
						</tr>
						<tr v-for="item in userInfo.level_list" :key="item.grade">
							<td>{{item.name}}</td>
							<td>{{item.growth_value}}</td>
							<td>{{item.discount}}</td>
						</tr>
					</table>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import MescrollCompMixin from "@/components/mescroll-uni/mixins/mescroll-comp";
	import FreeBlindBox from "@/components/free-blind-box/free-blind-box.vue";
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	import {
		getMenu,
		getGoodsList
	} from '@/api/store'
	import {
		toLogin
	} from '@/utils/login'
	import {
		menuJump,
		copy
	} from '@/utils/tools'
	import Cache from '@/utils/cache'
	const app = getApp()
	export default {
		mixins: [MescrollCompMixin],
		components: {FreeBlindBox},
		data() {
			return {
				showNav: false,
				navBg: 0,
				menuList: [],
				isShowFreeBlindBox: false,
				specificationPopup: false,
				vipImgUrl: [
					'/static/images/vip/vip-0.png',
					'/static/images/vip/vip-1.png',
					'/static/images/vip/vip-2.png',
					'/static/images/vip/vip-3.png',
					'/static/images/vip/vip-4.png',
					'/static/images/vip/vip-5.png',
					'/static/images/vip/vip-6.png',
				],
				freeGoodsList: [],
			};
		},

		onLoad(option) {
      this.getMenuFun();
			this.getGoodsLists();
		},

		onShow() {
			this.getUser();
			this.getCartNum()
		},

		onPageScroll(e) {
			const top = uni.upx2px(100)
			const {
				scrollTop
			} = e
			let percent = scrollTop / top > 1 ? 1 : scrollTop / top
			this.navBg = percent
		},
		onPullDownRefresh() {
			this.getUser().then(() => {
				uni.stopPullDownRefresh();
			})
			this.getMenuFun();
		},
		methods: {
			...mapActions(['getCartNum', 'getUser']),
			// 关闭免费盲盒弹窗
			closeFreeBlindBox() {
				this.isShowFreeBlindBox = false;
			},
			openFree() {
				this.isShowFreeBlindBox = true;
			},
			// 获取赠送商品列表
			getGoodsLists() {
				const obj = {
					page_no: 1,
					page_size: 1,
					keyword: '',
					type: '3'
				}
				getGoodsList(obj).then(res =>{
					this.freeGoodsList = res.data.lists;
				})
			},
			// 打开等级说明
			openLevel() {
				this.specificationPopup = true;
			},
			
			goLogin() {
				let {
					isLogin
				} = this;
				if (isLogin) {
					uni.navigateTo({
						url: '/bundle/pages/user_profile/user_profile'
					});
					return;
				}
				uni.navigateTo({
					url: '/pages/login/login'
				});
			},

			goPage(url) {
				if (!this.isLogin) return toLogin()
				uni.navigateTo({
					url
				});
			},
			async getMenuFun() {
				const {
					data,
					code
				} = await getMenu({
					type: 2,
				})
				if (code == 1) {
					this.menuList = data
				}
			},
			onCopy(e) {
				copy(this.userInfo.sn)
			},
			menuJump(item) {
				if(item.link_type == 2) {
					this.$toast({ title: '即将上线' })
				} else {
					menuJump(item)
				}
			}
		},
		computed: {
			...mapGetters(["userInfo", "inviteCode", 'appConfig']),
			background() {
				const {
					center_setting
				} = this.appConfig
				return center_setting.top_bg_image ? {
					'background-image': `url(${center_setting.top_bg_image})`
				} : {}
			},
		}
	};
</script>
<style lang="scss" scoped>
	.user {
		position: relative;
		height: 85vh;
		background-image: url(../../static/images/my_topbg.png);
		background-size: 100% auto;
		background-repeat: no-repeat;

		.header {
			margin: 0 20rpx;
			padding-top: 20rpx;

			.hd-wrap {
				padding-bottom: 20rpx;
				border-radius: 20rpx;
			}

			.user-info {
				padding: 30rpx;

				.avatar {
					height: 110rpx;
					width: 110rpx;
					border-radius: 50%;
					overflow: hidden;
				}

				.name {
					width: 100%;
					display: flex;
					text-align: left;
					margin-bottom: 5rpx;
					justify-content: space-between;
				}

				.user-id {
					border: $-solid-border;
					border-radius: 100rpx;
					padding: 2rpx 15rpx;
				}

				.user-opt {
					position: relative;

					.dot {
						position: absolute;
						background-color: #ee0a24;
						border: 2rpx solid #FFFFFF;
						color: $-color-primary;
						border-radius: 100%;
						top: 6rpx;
						right: 0rpx;
						font-size: 22rpx;
						min-width: 16rpx;
						height: 16rpx;
					}
				}

				.level {
					display: flex;
					align-items: center;
					color: #ffdea5;
					
					.level-name {
						margin-left: 20rpx;
						margin-right: 16rpx;
					}
				}
			}

			.user-assets {
				flex: 1;

				.user-assests-item {
					flex: 1;
				}
			}

		}

		.order-nav {
			.icon-contain {
				position: relative;
			}
		}

		.order-nav,
		.my-assets {
			margin: 20rpx 20rpx 0;
			border-radius: 8rpx;
		}

		.server-nav {
			margin: 20rpx;
			border-radius: 8rpx;
		}

		.title {
			height: 88rpx;
			padding: 0 30rpx;
			border-bottom: $-dashed-border;
		}

		.nav {
			padding: 26rpx 0 0;

			.assets-item {
				flex: 1;
			}

			.item {
				width: 25%;
			}

			.badge {
				padding: 0 6rpx;
				min-width: 28rpx;
				height: 28rpx;
				border-radius: 28rpx;
				box-sizing: border-box;
				border: 1rpx solid $-color-primary;
				color: $-color-primary;
				position: absolute;
				left: 33rpx;
				top: -10rpx;
				z-index: 2;
			}

			.nav-icon {
				width: 52rpx;
				height: 52rpx;
			}
		}
		.rule-box {
			position: relative;
			display: flex;
			flex-direction: column;
			align-items: center;
			margin: 0 12px;
			background-color: #fff;
			border-radius: 12rpx;
			overflow: hidden;
		
			.rule-title {
				width: 100%;
				font-size: 15px;
				padding: 12px 0;
				font-weight: 600;
				text-align: center;
				background-color: #ffe0df;
			}
		
			.rule-table {
				width: 100%;
				text-align: center;
				padding: 0;
				margin: 0;
				box-sizing: border-box;
		
				table {
					width: 100%;
					border-collapse: collapse;
				}
		
				th {
					border: 1px solid #e6e6e6;
					padding: 20rpx 32rpx;
				}
		
				td {
					border: 1px solid #e6e6e6;
					padding: 16rpx 0;
				}
			}
		
			.rule-attention {
				font-size: 8px;
				font-weight: 600;
				margin: 12px 0;
			}
		
			.close-icon {
				position: absolute;
				right: 12px;
				top: 8px;
		
				img {
					width: 15px;
				}
			}
		}
		
		.free-box {
			position: absolute;
			right: 20rpx;
			bottom: 0;
			@-webkit-keyframes roundRule {
			
				0%,
				100% {
			
					-webkit-transform: rotate(-15deg);
			
				}
			
				50% {
			
					-webkit-transform: rotate(15deg);
			
				}
			
			}
			
			-webkit-animation: roundRule 1s ease-in-out infinite;
			
			-webkit-transform-origin: center center;
		}
	}
</style>
